strip-json-comments.js ➔ stripJsonComments   F
last analyzed

Complexity

Conditions 17
Paths 23

Size

Total Lines 47
Code Lines 34

Duplication

Lines 1
Ratio 2.13 %

Importance

Changes 0
Metric Value
cc 17
eloc 34
nc 23
nop 1
dl 1
loc 47
rs 1.8
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like strip-json-comments.js ➔ stripJsonComments often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/*!
2
	strip-json-comments
3
	Strip comments from JSON. Lets you use comments in your JSON files!
4
	https://github.com/sindresorhus/strip-json-comments
5
	by Sindre Sorhus
6
	MIT License
7
*/
8
(function () {
9
	'use strict';
10
11
	function stripJsonComments(str) {
12
		var currentChar;
13
		var nextChar;
14
		var insideString = false;
15
		var insideComment = false;
16
		var ret = '';
17
18
		for (var i = 0; i < str.length; i++) {
19
			currentChar = str[i];
20
			nextChar = str[i + 1];
21
22
			if (!insideComment && str[i - 1] !== '\\' && currentChar === '"') {
23
				insideString = !insideString;
24
			}
25
26
			if (insideString) {
27
				ret += currentChar;
28
				continue;
29
			}
30
31
			if (!insideComment && currentChar + nextChar === '//') {
32
				insideComment = 'single';
33
				i++;
0 ignored issues
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable i here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
34
			} else if (insideComment === 'single' && currentChar + nextChar === '\r\n') {
35
				insideComment = false;
36
				i++;
0 ignored issues
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable i here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
37 View Code Duplication
			} else if (insideComment === 'single' && currentChar === '\n') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
38
				insideComment = false;
39
			} else if (!insideComment && currentChar + nextChar === '/*') {
40
				insideComment = 'multi';
41
				i++;
0 ignored issues
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable i here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
42
				continue;
43
			} else if (insideComment === 'multi' && currentChar + nextChar === '*/') {
44
				insideComment = false;
45
				i++;
0 ignored issues
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable i here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
46
				continue;
47
			}
48
49
			if (insideComment) {
50
				continue;
51
			}
52
53
			ret += currentChar;
54
		}
55
56
		return ret;
57
	}
58
59
	if (typeof module !== 'undefined' && module.exports) {
60
		module.exports = stripJsonComments;
61
	} else {
62
		window.stripJsonComments = stripJsonComments;
63
	}
64
})();
65